package com.cx.webflux.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;

/**
 * 响应式控制层
 *
 * @author 旭旭
 * @create 2018-07-30 21:27
 **/
@RestController
public class HelloController {

    @GetMapping("/hello")
    public Mono<String> hello(){
        return Mono.just("Welcome to reactive world ~");
    }

    HandlerFunction<ServerResponse> timeFunction =
            request -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(
                    Mono.just("Now is " + new SimpleDateFormat("HH:mm:ss").format(new Date())),String.class
            );

    HandlerFunction<ServerResponse> dateFunction =
            request -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(
                    Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd ").format(new Date())),String.class
            );

    RouterFunction<ServerResponse> router = RouterFunctions.route(GET("/time"),timeFunction)
            .andRoute(GET("/date"),dateFunction);

}
